package com.dao.Impl;

import com.dao.ResourceDao;
import com.entity.Resource;
import com.entity.Staff;
import com.util.JDBC;
import com.util.TransBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ResourceDaoImpl implements ResourceDao {
    @Override
    public int insert(Resource resource) {
        return 0;
    }

    @Override
    public int update(Resource resource) {
        return 0;
    }

    @Override
    public int delete(int id) {
        return 0;
    }

    @Override
    public List<Resource> findAll() {
        return null;
    }

    @Override
    public List<Resource> findByStaffId(Staff staff) {
        //查询主权限
        String sql = "select res.id,resource_name resourceName,res.url from staff st,role," +
                "resource_role rr,resource res where st.id = ? and st.roleId = role.id " +
                "and role.id = rr.role_id and rr.resource_id = res.id and res.status=1 and res.pid = 0";
        List<Map> map = JDBC.selectMap(sql, staff.getId());
        List<Resource> list = TransBean.populate(Resource.class, map);
        if (list != null && list.size() > 0) {
            for (Resource resource : list) {
                sql = "SELECT\n" +
                        "\tres.id,\n" +
                        "\tresource_name resourceName,\n" +
                        "\tres.url \n" +
                        "FROM\n" +
                        "\tstaff st,\n" +
                        "\trole,\n" +
                        "\tresource_role rr,\n" +
                        "\tresource res \n" +
                        "WHERE\n" +
                        "\tst.id = ? \n" +
                        "\tAND st.roleId = role.id \n" +
                        "\tAND roleid = rr.role_id \n" +
                        "\tAND rr.resource_id = res.id \n" +
                        "\tAND res.STATUS = 1 \n" +
                        "\tAND res.pid = ?\n" +
                        "\tand res.id=rr.resource_id\n" +
                        "\tORDER BY sort ";
                List<Map> lists = JDBC.selectMap(sql, staff.getId(),resource.getId());
                resource.setList(TransBean.populate(Resource.class, lists));
            }
        }
        return list;
    }

    @Override
    public List<Integer> findByRoleId(int roleId) {
        //查询主权限
        String sql = "select resource_id id from resource_role " +
                " where  role_id=?";
        List<Map> map = JDBC.selectMap(sql, roleId);
        List<Resource> list = TransBean.populate(Resource.class, map);
        ArrayList arrayList = new ArrayList<Integer>();
        if(list!=null&&list.size()>0){
            for (Resource resource : list) {
                arrayList.add(resource.getId());
            }
            return arrayList;
        }else{
            return null;
        }
    }

    @Override
    public List<Resource> findByResource() {
        //查询主权限
        String sql = " select * from resource where status=1 and pid = 0";
        List<Map> map = JDBC.selectMap(sql);
        List<Resource> list = TransBean.populate(Resource.class, map);

        for (Resource resource : list) {
            sql = "select id,resource_name resourceName,url from resource where pid=?";
            List<Map> lists = JDBC.selectMap(sql, resource.getId());
            resource.setList(TransBean.populate(Resource.class, lists));
        }
        return list;
    }
}
